using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace MemGuardian
{
    /// <summary>
    /// 圆形进度条控件
    /// 自定义绘制圆形进度条，支持颜色渐变、阴影效果和文本显示
    /// 主要用于显示内存使用率等系统状态信息
    /// </summary>
    public class CircularProgressBar : Control
    {
        #region 私有字段

        /// <summary>
        /// 当前进度值
        /// </summary>
        private int _value = 0;

        /// <summary>
        /// 最大值
        /// </summary>
        private int _maximum = 100;

        /// <summary>
        /// 进度条颜色
        /// </summary>
        private Color _progressColor = Color.FromArgb(76, 175, 80);

        /// <summary>
        /// 背景圆环颜色
        /// </summary>
        private Color _backgroundColor = Color.FromArgb(0, 188, 212); // #00BCD4

        /// <summary>
        /// 文本颜色
        /// </summary>
        private Color _textColor = Color.FromArgb(76, 175, 80);

        /// <summary>
        /// 圆环线条宽度
        /// </summary>
        private int _lineWidth = 8;

        /// <summary>
        /// 百分比文本字体
        /// </summary>
        private Font _textFont = new Font("Microsoft YaHei", 18, FontStyle.Bold);

        /// <summary>
        /// 标签文本字体
        /// </summary>
        private Font _labelFont = new Font("Microsoft YaHei", 10, FontStyle.Regular);

        #endregion

        #region 构造函数

        /// <summary>
        /// CircularProgressBar构造函数
        /// 初始化控件样式，启用双缓冲防止闪烁
        /// </summary>
        public CircularProgressBar()
        {
            // 启用双缓冲防止闪烁
            SetStyle(ControlStyles.AllPaintingInWmPaint | 
                     ControlStyles.UserPaint | 
                     ControlStyles.DoubleBuffer | 
                     ControlStyles.ResizeRedraw, true);
            UpdateStyles();
        }

        #endregion

        #region 公共属性

        /// <summary>
        /// 当前进度值
        /// 设置时会自动限制在0到Maximum之间，并触发重绘
        /// </summary>
        public int Value
        {
            get { return _value; }
            set
            {
                // 确保值在有效范围内
                int newValue = Math.Max(0, Math.Min(value, _maximum));
                if (_value != newValue)
                {
                    _value = newValue;
                    Invalidate(); // 触发重绘
                }
            }
        }

        /// <summary>
        /// 最大值
        /// 设置时会触发重绘
        /// </summary>
        public int Maximum
        {
            get { return _maximum; }
            set
            {
                _maximum = value;
                Invalidate(); // 触发重绘
            }
        }

        /// <summary>
        /// 进度条颜色
        /// 设置时会触发重绘
        /// </summary>
        public Color ProgressColor
        {
            get { return _progressColor; }
            set
            {
                _progressColor = value;
                Invalidate(); // 触发重绘
            }
        }

        /// <summary>
        /// 背景圆环颜色
        /// 设置时会触发重绘
        /// </summary>
        public Color BackgroundColor
        {
            get { return _backgroundColor; }
            set
            {
                _backgroundColor = value;
                Invalidate(); // 触发重绘
            }
        }

        /// <summary>
        /// 文本颜色
        /// 设置时会触发重绘
        /// </summary>
        public Color TextColor
        {
            get { return _textColor; }
            set
            {
                _textColor = value;
                Invalidate(); // 触发重绘
            }
        }

        /// <summary>
        /// 圆环线条宽度
        /// 设置时会触发重绘
        /// </summary>
        public int LineWidth
        {
            get { return _lineWidth; }
            set
            {
                _lineWidth = value;
                Invalidate(); // 触发重绘
            }
        }

        /// <summary>
        /// 标签文本
        /// 显示在进度条下方的描述文字
        /// </summary>
        public string LabelText { get; set; } = "内存使用率";

        #endregion

        #region 私有辅助方法

        /// <summary>
        /// 根据进度值获取进度条颜色
        /// 实现颜色渐变效果：绿色->橙色->红色
        /// </summary>
        /// <param name="value">进度值</param>
        /// <returns>对应的颜色</returns>
        private Color GetProgressColor(int value)
        {
            if (value <= 50)
                return Color.FromArgb(76, 175, 80); // 绿色 #4CAF50 - 低负载
            else if (value <= 80)
                return Color.FromArgb(255, 128, 64); // 橙色 #ff8040 - 中等负载
            else
                return Color.FromArgb(255, 87, 34); // 红色 #FF5722 - 高负载
        }

        /// <summary>
        /// 根据进度值获取文本颜色
        /// 与进度条颜色保持一致的渐变效果
        /// </summary>
        /// <param name="value">进度值</param>
        /// <returns>对应的文本颜色</returns>
        private Color GetTextColor(int value)
        {
            if (value < 70)
                return Color.FromArgb(76, 175, 80); // 绿色 #4CAF50 - 低负载
            else if (value < 90)
                return Color.FromArgb(255, 128, 64); // 橙色 #ff8040 - 中等负载
            else
                return Color.FromArgb(244, 67, 54); // 红色 #FF5722 - 高负载
        }

        #endregion

        #region 绘制方法

        /// <summary>
        /// 重写OnPaint方法，自定义绘制圆形进度条
        /// 包括圆环、进度条、阴影效果、文本等元素的绘制
        /// </summary>
        /// <param name="e">绘制事件参数</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias; // 启用抗锯齿，使绘制更平滑
            
            // 计算绘制区域，为线条宽度和阴影预留空间
            int padding = _lineWidth + 4; // 线条宽度 + 阴影偏移
            int diameter = Math.Min(Width, Height) - padding * 2;
            int x = (Width - diameter) / 2;
            int y = (Height - diameter) / 2;
            
            Rectangle rect = new Rectangle(x, y, diameter, diameter);
            
            // 绘制外圆环阴影，增加立体效果
            Rectangle shadowRect = new Rectangle(x + 2, y + 2, diameter, diameter);
            using (Pen shadowPen = new Pen(Color.FromArgb(50, 0, 0, 0), _lineWidth))
            {
                g.DrawArc(shadowPen, shadowRect, 0, 360);
            }
            
            // 绘制背景圆环（完整的空心圆圈）
            using (Pen backgroundPen = new Pen(_backgroundColor, _lineWidth))
            {
                g.DrawArc(backgroundPen, rect, 0, 360);
            }
            
            // 绘制进度圆环（根据当前值显示部分圆环）
            if (_value > 0)
            {
                float sweepAngle = (float)_value / _maximum * 360;
                using (Pen progressPen = new Pen(_progressColor, _lineWidth))
                {
                    progressPen.StartCap = LineCap.Round; // 圆角开始
                    progressPen.EndCap = LineCap.Round;   // 圆角结束
                    // 确保sweepAngle至少为1度，以便显示进度
                    if (sweepAngle < 1) sweepAngle = 1;
                    g.DrawArc(progressPen, rect, -90, sweepAngle); // 从顶部开始绘制
                }
            }
            
            // 绘制内部实心圆（带阴影效果）
            int innerDiameter = diameter - _lineWidth * 2 - 20;
            int innerX = (Width - innerDiameter) / 2;
            int innerY = (Height - innerDiameter) / 2;
            Rectangle innerRect = new Rectangle(innerX, innerY, innerDiameter, innerDiameter);
            
            // 内部圆的阴影
            Rectangle innerShadowRect = new Rectangle(innerX + 3, innerY + 3, innerDiameter, innerDiameter);
            using (Brush shadowBrush = new SolidBrush(Color.FromArgb(30, 0, 0, 0)))
            {
                g.FillEllipse(shadowBrush, innerShadowRect);
            }
            
            // 内部实心圆（白色背景）
            using (Brush innerBrush = new SolidBrush(Color.White))
            {
                g.FillEllipse(innerBrush, innerRect);
            }
            
            // 内部圆的边框
            using (Pen innerBorderPen = new Pen(Color.FromArgb(200, 200, 200), 1))
            {
                g.DrawEllipse(innerBorderPen, innerRect);
            }
            
            // 绘制百分比文字（居中显示）
            string percentageText = $"{_value}%";
            SizeF textSize = g.MeasureString(percentageText, _textFont);
            PointF textPoint = new PointF(
                Width / 2 - textSize.Width / 2,
                Height / 2 - textSize.Height / 2 - 10
            );
            
            // 使用外部设置的文字颜色
            using (Brush textBrush = new SolidBrush(_textColor))
            {
                g.DrawString(percentageText, _textFont, textBrush, textPoint);
            }
            
            // 绘制标签文字（显示在进度条下方）
            SizeF labelSize = g.MeasureString(LabelText, _labelFont);
            PointF labelPoint = new PointF(
                Width / 2 - labelSize.Width / 2,
                Height / 2 + 8
            );
            
            using (Brush labelBrush = new SolidBrush(Color.Gray))
            {
                g.DrawString(LabelText, _labelFont, labelBrush, labelPoint);
            }
        }

        #endregion
    }
}
